/**
 * 
 */
package org.gov.vis.master.controller;

import java.util.List;

import org.gov.vis.common.exceptions.InvalidInputException;
import org.gov.vis.common.exceptions.SystemException;
import org.gov.vis.master.dao.StaffDao;
import org.gov.vis.master.dao.StaffDaoImpl;
import org.gov.vis.master.dto.StaffDto;
import org.gov.vis.util.DBConstants;
import org.gov.vis.util.DU;
import org.gov.vis.util.GuiConstants;
import org.gov.vis.util.ValidatorUtils;

/**
 * @author Vishwanath
 *
 */
public class StaffCtlrImpl implements StaffCtlr{

	/**
	 * Method called to Add or Edit the Staff
	 * @param staffDto
	 * @throws Exception
	 */
	@Override
	public void addOrEditStaff(final StaffDto _staffDto) throws Exception {
		try{
			/*
			 * Below set of validations check for Required/Mandatory fields
			 */
			String _staffCodeErr = ValidatorUtils.validateString(rb.getString("dialog_label_staff_code"), _staffDto.getCode(), 1, 20, true);
			if(_staffCodeErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_code").toUpperCase(), _staffCodeErr);

			String _staffNameErr = ValidatorUtils.validateString(rb.getString("dialog_label_staff_name"), _staffDto.getName(), 3, 50, true);
			if(_staffNameErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_name").toUpperCase(), _staffNameErr);

			String _staffFatherNameErr = ValidatorUtils.validateString(rb.getString("dialog_label_staff_fathername"), _staffDto.getFatherName(), 3, 50, true);
			if(_staffFatherNameErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_fathername").toUpperCase(), _staffFatherNameErr);

			String _staffDOBErr = ValidatorUtils.validateString(rb.getString("dialog_label_staff_dob"), DU.format(_staffDto.getDob(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_staffDOBErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_dob").toUpperCase(), _staffDOBErr);

			String _staffDOJErr = ValidatorUtils.validateString(rb.getString("dialog_label_staff_dofjoin"), DU.format(_staffDto.getDoj(), DBConstants.DISPLAY_DATE_FORMAT), 6, 50, true);
			if(_staffDOJErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_dofjoin").toUpperCase(), _staffDOJErr);

			String _staffQualfErr = ValidatorUtils.validateString(rb.getString("dialog_label_staff_qualfcation"), _staffDto.getQualificatiion(), 3, 50, true);
			if(_staffQualfErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_qualfcation").toUpperCase(), _staffQualfErr);

			String _staffDsgnErr = ValidatorUtils.validateString(rb.getString("dialog_label_staff_designation"), _staffDto.getDesignation(), 3, 50, true);
			if(_staffDsgnErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_designation").toUpperCase(), _staffDsgnErr);


			String _staffJjobTypeErr = ValidatorUtils.validateDropDown(rb.getString("dialog_label_staff_emptype"), Long.valueOf(_staffDto.getEmploymentType()), 1);
			if(_staffJjobTypeErr != null)
				errorMsgs.put(rb.getString("dialog_label_staff_emptype").toUpperCase(), _staffJjobTypeErr);

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			/*
			 * Below set of validations check for duplicate records
			 */
			//check if there exists anstaff Staff Object with the same Code
			//If Code has been duplicated, then throw the alert
			StaffDto staffDto = null;
			if(_staffDto.getStaffId() > 0){
				//get the StaffDto on the passed Code name
				staffDto = this.getStaffOnCode(_staffDto.getCode());
				if(staffDto != null && staffDto.getStaffId() != _staffDto.getStaffId()){
					errorMsgs.put(rb.getString("dialog_label_staff_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_STAFF_MSG));
				}
			}//end if
			else{
				//get the StaffDto on the passed Code name
				staffDto = this.getStaffOnCode(_staffDto.getCode());
				if(staffDto != null){
					errorMsgs.put(rb.getString("dialog_label_staff_code").toUpperCase(), rb.getString(GuiConstants.MASTER_DUPLICATE_STAFF_MSG));
				}
			}//end else

			if (errorMsgs != null && !errorMsgs.isEmpty())
				throw new InvalidInputException();

			final StaffDao _staffDao = new StaffDaoImpl();
			//add/edit the Staff
			_staffDao.addOrEditStaff(_staffDto);
		}
		catch(InvalidInputException ie){
			throw ie;
		}
		catch (Exception ex) {
			ex.printStackTrace();
			if(_staffDto.getStaffId() > 0){
				throw new SystemException(rb.getString(GuiConstants.MASTER_EDIT_STAFF_EXCEPTION));
			}else{
				throw new SystemException(rb.getString(GuiConstants.MASTER_ADD_STAFF_EXCEPTION));
			}
		}
	}//end addOrEditStaff

	/**
	 * Method called to delete the Staff Object based on the passed staff Id
	 * @param _staffId
	 * @throws Exception
	 */
	@Override
	public void deleteStaff(final long _staffId) throws Exception {
		try{
			final StaffDao _staffDao = new StaffDaoImpl();
			_staffDao.deleteStaff(_staffId);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_DELETE_STAFF_EXCEPTION));
		}
	}//end deleteStaff

	/**
	 * Method called to get the Staff based on the passed primary key Id
	 * @param _staffId
	 * @return
	 * @throws Exception
	 */
	@Override
	public StaffDto getStaff(final long _staffId) throws Exception {
		try{
			final StaffDao _staffDao = new StaffDaoImpl();
			StaffDto _staffDto = new StaffDto();
			_staffDto.setStaffId(_staffId);

			StaffDto staffDto = null;
			List<StaffDto> _staffList = _staffDao.getStaffs(_staffDto);
			if(_staffList != null && !_staffList.isEmpty()){
				staffDto = _staffList.get(0);
			}

			return staffDto;
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_EDIT_STAFF_EXCEPTION));
		}
	}//end getStaff

	/**
	 * Method called to get the Staff based on the passed Code
	 * @param _staffCode
	 * @return
	 * @throws Exception
	 */
	@Override
	public StaffDto getStaffOnCode(final String _code) throws Exception {
		final StaffDao _staffDao = new StaffDaoImpl();
		StaffDto _staffDto = new StaffDto();
		_staffDto.setCode(_code);

		StaffDto staffDto = null;
		List<StaffDto> _staffList = _staffDao.getStaffs(_staffDto);
		if(_staffList != null && !_staffList.isEmpty()){
			staffDto = _staffList.get(0);
		}

		return staffDto;
	}//end getStaffOnCode

	/**
	 * Method called to get the Staff based on the passed Staff Object
	 * @param staffDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public StaffDto getStaff(final StaffDto _staffDto) throws Exception {
		final StaffDao _staffDao = new StaffDaoImpl();

		StaffDto staffDto = null;
		List<StaffDto> _staffList = _staffDao.getStaffs(_staffDto);
		if(_staffList != null && !_staffList.isEmpty()){
			staffDto = _staffList.get(0);
		}

		return staffDto;
	}//end getStaffs

	/**
	 * Method called to get the list of Staff based on the passed Staff Object
	 * @param staffDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<StaffDto> getStaffs(final StaffDto _staffDto) throws Exception {
		final StaffDao _staffDao = new StaffDaoImpl();
		return _staffDao.getStaffs(_staffDto);
	}//end getStaffs

	/**
	 * Method called to search for the Staffs based on the passed Staff Object
	 * @param staffDto
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<StaffDto> searchStaffs(StaffDto _staffDto) throws Exception {
		try{
			final StaffDao _staffDao = new StaffDaoImpl();
			return _staffDao.searchStaffs(_staffDto);
		}
		catch (Exception ex) {
			throw new SystemException(rb.getString(GuiConstants.MASTER_VIEW_MAINTAIN_STAFF_EXCEPTION));
		}
	}//end searchStaffs

}//end class
